//debugger;

jQuery.fn.center = function () {
    this.css("position","fixed");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2)  + "px");
    return this;
};

jQuery.appDialog=function(url){
	var dlg={};
	(function(dlg){
		dlg.overlay=$('<div style="position: fixed; top:0px; left: 0px; width: 100%; height: 100%; overflow: hidden; background-color: #000; opacity: 0.5; z-index: 1000000;background: -webkit-linear-gradient(top, rgba(0,0,0,0.3) 0%,rgba(0,0,0,0.85) 48%,rgba(0,0,0,0.3) 100%);" />');
		dlg.dialog=$('<div style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); width: 300px; height: 200px; z-index: 1000001; position: fixed; top: 73px; left: 561.5px; background-position: initial initial; background-repeat: initial initial; box-shadow: 0 0 50px #000;padding: 10px; border-radius: 8px;"></div>');
		dlg.closeButton=$('<div style="width:10px; height=10px; padding: 0px 0px 3px 3px; position: absolute;  top: -7px; right: -6px; background-color: #333; color: #FFF; border: 2px solid #FFF; border-radius: 9px;height: 10px;font-size: 10px;cursor:  pointer;box-shadow: 0 0 5px #000;">X</div>').bind('click',function(){dlg.close();});
		dlg.heading=$('<div style="background-color: #CCC; padding: 8px;margin: -9px -9px 0px -9px;border-radius: 8px 8px 0px 0px;font-family: Verdana;font-size:  12px;text-shadow: 0 0 1px #666;">heading</div>');
		dlg.content=$('<div></div>');
		dlg.dialog.append(dlg.closeButton).append(dlg.heading).append(dlg.content);
		$('body').append(dlg.overlay).append(dlg.dialog);
		dlg.dialog.center();

		dlg.close=function(){
			dlg.overlay.remove();
			dlg.dialog.remove();
			keys.setActive('main');
		}
	})(dlg);

	return dlg;
}

var app=new function(){
	var dlg;
	this.createNewShortcut=function(){
		dlg=$.appDialog();
		keys.makeMap('dialog',{
			'esc':dlg.close,
			'ctrl.space':dlg.close
		}).setActive('dialog');
	}
	this.close=function(){
		if(dlg)dlg.close();
	}
}

var keys=new function(){
	var activeMap=this.activeMap=null;
	var maps=this.maps={}

	this.getMap=function(name){
		return this.exists(name)?maps[name]:{};
	}

	this.getActiveMap=function(){
		return this.exists(activeMap)?maps[activeMap]:{};
	}

	this.makeMap=function(name,map){
		maps[name]=map;
		if(!activeMap)activeMap=name;
		return this;
	}

	this.setActive=function(name){
		if(this.exists(name))activeMap=name;
		return this;
	}

	this.exists=function(name){
		return typeof(maps[name])=='object';
	}
}

keys.makeMap('main',{
	'ctrl.space' : app.createNewShortcut,
	'ctrl.shift.space' : app.close
});

